//
//  ZiXunViewController.m
//  jdd
//
//  Created by admin on 14-7-24.
//
//

#import "ZiXunViewController.h"
#import "JddGlobalParameter.h"
#import "CellModel.h"
#import "CustomCell.h"
#import "ZixunDetailViewController.h"
#import "JddGlobal.h"

@interface ZiXunViewController ()
{
    UIButton *leftBtn;
    UIButton *rightBtn;
    UILabel *pagelabel;
}

@end

@implementation ZiXunViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    ziXunTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -35, self.view.frame.size.width, self.view.frame.size.height-40) style:UITableViewStyleGrouped];
    ziXunTableView.delegate = self;
    ziXunTableView.dataSource = self;
    ziXunTableView.separatorStyle = NO;
    [self.view addSubview:ziXunTableView];
    
    dataArray = [[NSMutableArray alloc] init];
    pageNum = 1;
    
    JddNoticeOperation *operation = [[JddNoticeOperation alloc] init];
	operation.delegate = self;
	[[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-110, self.view.frame.size.width, 50)];
    bottomView.backgroundColor = [UIColor colorWithRed:210/255.0 green:230/255.0f blue:230/255.0f alpha:1];
    [self.view addSubview: bottomView];
    
    //添加左按钮
    leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(90, 15, 12, 15)];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"adv_home_left_normal"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:leftBtn];
    
    //添加右按钮
    rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(210, 15, 12, 15)];
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"adv_home_right_pressed"]
                                            forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightBtnClick:)
                   forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:rightBtn];
    
    //当前页的page左
    pagelabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 15, 12, 15)];
    pagelabel.backgroundColor = [UIColor clearColor];
    pagelabel.text = [NSString stringWithFormat:@"%d",pageNum];
    pagelabel.textColor = [UIColor redColor];
    [bottomView addSubview:pagelabel];
    
    //总的页数右
    UILabel *allPageLab = [[UILabel alloc] initWithFrame:CGRectMake(170, 15, 12, 15)];
    allPageLab.backgroundColor = [UIColor clearColor];
    allPageLab.text = @"5";
    [bottomView addSubview:allPageLab];
    
    UILabel *midLabel = [[UILabel alloc] initWithFrame:CGRectMake(155, 15, 12, 15)];
    midLabel.backgroundColor = [UIColor clearColor];
    midLabel.text = @"/";
    [bottomView addSubview:midLabel];
    
}

- (void)leftBtnClick:(UIButton *)button
{
    [ziXunTableView setContentOffset:CGPointMake(0, 0) animated:NO];
    if (pageNum > 1) {
        pageNum --;
        pagelabel.text = [NSString stringWithFormat:@"%d",pageNum];
        [ziXunTableView reloadData];
    }if (pageNum != 5) {
        [rightBtn setBackgroundImage:[UIImage imageNamed:@"adv_home_right_pressed"] forState:UIControlStateNormal];
    }
    if (pageNum == 1) {
        [leftBtn setBackgroundImage:[UIImage imageNamed:@"adv_home_left_normal"] forState:UIControlStateNormal];
    }

}

- (void)rightBtnClick:(UIButton *)button
{
    [ziXunTableView setContentOffset:CGPointMake(0, 0) animated:NO];
    if (pageNum < 5) {
        pageNum ++;
        pagelabel.text = [NSString stringWithFormat:@"%d",pageNum];
        [ziXunTableView reloadData];
        if (pageNum != 1) {
            [leftBtn setBackgroundImage:[UIImage imageNamed:@"adv_home_left_pressed"] forState:UIControlStateNormal];
        }
        if (pageNum == 5) {
            [rightBtn setBackgroundImage:[UIImage imageNamed:@"adv_home_right_normal"] forState:UIControlStateNormal];
        }
    }
}

//公告回调方法
- (void)NoticeOperationResult:(NSData *)data
{
    if (data) {
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        if ([result isKindOfClass:[NSDictionary class]]) {
            
            NSDictionary *dic = (NSDictionary *)result;
            NSString *dictStr = [dic objectForKey:@"Data"];
            
            //用dataUsingEncoding进行编码
            NSData *data1 = [dictStr dataUsingEncoding:NSUTF8StringEncoding];
            //用字典取出编码后的数据
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data1 options:NSJSONReadingMutableContainers error:nil];
            for (NSDictionary *numDic in [dict objectForKey:@"item"]){
                CellModel *model = [[CellModel alloc] init];
                model.ID = [numDic objectForKey:@"ID"];
                model.DateTime = [numDic objectForKey:@"DateTime"];
                model.headerTitle = [numDic objectForKey:@"Title"];
                [dataArray addObject:model];
              //  NSLog(@"--------------dataArray = %d",dataArray.count);
            }
        }
        [ziXunTableView reloadData];
    }
}

#pragma mark =====tableView======
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return [dataArray count];
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 52;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (dataArray.count == 0) {
        static NSString *cellName = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
        }
        return cell;
    }
    else{
        
        static NSString *cellName = @"cellname";
        CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]lastObject];
            CellModel *model = [dataArray objectAtIndex:indexPath.row + (pageNum-1) *20];
            cell.headerTitle.text = model.headerTitle;
            cell.dateTitle.text = model.DateTime;
             cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"320x60.png"]];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    }
}

//===========cell被点击的＝＝＝＝＝＝＝＝＝＝＝
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZixunDetailViewController *detailView = [[ZixunDetailViewController alloc] init];
    CellModel *model = [dataArray objectAtIndex:indexPath.row + (pageNum-1) *20];
    detailView.noticeId = model.ID;
    [self.navigationController pushViewController:detailView animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    //隐藏tabbar
    [self setJddTabBarHidden:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
